package com.ys.controller;


import com.ys.pojo.AllocationRecord;
import com.ys.pojo.Drugs;
import com.ys.pojo.Warehouse;
import com.ys.service.AllocationService;
import com.ys.service.DrugsService;
import com.ys.service.WarehouseService;
import com.ys.vo.AllocationVo;
import com.ys.vo.DrugAddVo;
import com.ys.vo.ResultVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.List;

@RestController
@RequestMapping("/kucun")
public class ZKucunController {

    @Resource
    private WarehouseService warehouseService;      //仓库service
    @Resource
    private DrugsService drugsService;              //药品id

    @Resource
    private AllocationService allocationService;        //调拨记录



    @GetMapping("/test")
    public ModelAndView test(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/kucun/test");
        return mav;
    }




    /**
     * 跳转到调库
     * @return 用modelandview传过去个url
     */
    @GetMapping("/allocation")
    public ModelAndView allocation(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/kucun/allocation");
        return mav;
    }

    /**
     * 查询了可用仓库
     * @return 返回可用仓库集合
     */
    @GetMapping("/searchWare")
    public ResultVo searchWare(){
        List<Warehouse> list = warehouseService.searchStatic();
        return ResultVo.success(list);
    }

    /**
     * 药品查询
     * @return
     */
    @GetMapping("/searchDrug")
    public ResultVo searchDrug(){
        List<Drugs> list = drugsService.searchAll();
        return ResultVo.success(list);
    }


    /**
     * 根据仓库id查药品
     * @param wId 仓库id
     * @return  药品集合
     */
    @GetMapping("/searchTerm")
    public ResultVo searchTerm(Integer wId){
        List<Drugs> list = drugsService.searchWareId(wId);
        return ResultVo.success(list);
    }

    @PostMapping(value = "/doUpdNum", produces = "application/json;charset=utf-8")
    public ResultVo doUpdNum(@RequestBody AllocationVo allocationVo){
        int result = drugsService.searchExist(allocationVo.getCallIn(),allocationVo.getDrugsId());
        if (result>0){
            try {
                drugsService.updNumber(allocationVo);
                return ResultVo.success("调入成功!!");
            } catch (Exception e) {
                return ResultVo.failure("调入失败...");
            }

        }else{
            Drugs drugs = drugsService.searchDrugsId(allocationVo.getDrugsId());
            drugs.setDrugsid(drugsService.searchDrugsIdMax()+1);
            drugs.setNumber(allocationVo.getNumber());
            drugs.setBitid(allocationVo.getCallIn());
            boolean isResult = drugsService.addNew(drugs,allocationVo);

            if (isResult){
                drugsService.updNumber(allocationVo);
                return ResultVo.success("调入成功!!");
            }
            return ResultVo.failure("调入失败...");
        }
    }



    @GetMapping("/searchRecord")
    public ResultVo searchRecord(){
        List<AllocationRecord> list = allocationService.searchAll();
        return ResultVo.success(list);
    }

}
